package com.maibow.base.defines;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maibow.dao.DaoSupport;
import com.maibow.entity.Page;
import com.maibow.entity.SConfigure;
import com.maibow.util.PageData;
/**
 * 
 * @author Roy
 *
 */
public class ParameterReadService {
	
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	/**
	 * 记录所有的系统配置信息
	 */
	private List<SConfigure> sconfigures ;
	
	/**
	 * 初始化函数
	 */
	public void init() {
		try {
			sconfigures =  (List<SConfigure>)dao.findForList("SconfigureMapper.listAll",new HashMap());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void initSConfigure() {
		List<SConfigure> confs = this.getAllSConfigures();
		GlobalContext.CONFIG_INFO = new HashMap<String,String>();
		for(SConfigure sc : confs) GlobalContext.CONFIG_INFO.put(sc.getScode(), sc.getSvalue());
		
	}
	
	public List<SConfigure> getSConfigure(String code) {
		List<SConfigure> scs = new ArrayList<SConfigure>();
		if(code == null) return scs;
		for(SConfigure sc : sconfigures) {
			if(code.equalsIgnoreCase(sc.getScode())) scs.add(sc);
		}
		return scs;
	}
	
	public List<SConfigure> getAllSConfigures() {
		return sconfigures;
	}

	public void refreshAll() {
		this.init();
		this.initSConfigure();
	}

	public List<PageData> querSconfigureslistPageWithConfigureName(Page page) {
		 try {
			return (List<PageData>) dao.findForList("SconfigureMapper.querSconfigureslistPageWithConfigureName", page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public PageData findByParams(PageData pd) {
		try {
			return (PageData) dao.findForObject("SconfigureMapper.findByParams", pd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Object findCodeCount(PageData pd) {
		try {
			return (PageData) dao.findForObject("SconfigureMapper.findCodeCount", pd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//新增
	public void save(PageData pd)throws Exception{
		dao.save("SconfigureMapper.save", pd);
	}
	
	//修改
	public void edit(PageData pd)throws Exception{
		dao.update("SconfigureMapper.edit", pd);
	}

	public void delete(PageData pd) {
		try {
			dao.delete("SconfigureMapper.delete", pd);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
